package com.corner.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.App;
import com.corner.model.cornerservice.domain.Attention;
import com.corner.model.cornerservice.domain.Location;
import com.corner.presenter.imp.AttentionItemPresenter;
import com.corner.ui.activity.ChannelActivity;
import com.corner.ui.activity.ChatActivity;
import com.corner.ui.activity.ProfileActivity;
import com.corner.ui.activity.SelectContactActivity;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.view.IAttentionItemView;
import com.corner.utils.ToastUtil;
import com.corner.utils.view.FindView;
import com.corner.utils.view.InjectViewUtils;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * Created by Administrator on 2015/7/25，11.41.
 * 描述：
 */
public class AttentionAdapter extends BaseSwipeAdapter implements IAttentionItemView, View.OnClickListener {
    private AttentionItemPresenter presenter;
    private Context context;
    private List<Attention> attentions;

    public AttentionAdapter(Context context, List<Attention> attentions) {
        this.context = context;
        this.attentions = attentions;
        presenter = new AttentionItemPresenter();
        presenter.setView(this);
    }

    @Override
    public int getSwipeLayoutResourceId(int i) {
        return R.id.swipe_frag_session;
    }

    @Override
    public View generateView(int i, ViewGroup viewGroup) {
        View convertView = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.lv_attention_city_list_item, null);
            Holder holder = new Holder();
            InjectViewUtils.injectView(holder, convertView, context);
            convertView.setTag(holder);

            SwipeLayout swipeLayout = holder.swipeLayout;
            swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
            swipeLayout.addDrag(SwipeLayout.DragEdge.Right, holder.llDrag);
            swipeLayout.setClickToClose(true);
            swipeLayout.setDragDistance(300);
        }

        return convertView;
    }

    @Override
    public void setUser(Location location, Holder holder) {
        if (holder.location != null && location.getObjectId().equals(holder.location.getObjectId())) {
            if (location.user != null) {
                if (location.user.getAvaterUrl() != null) {
                    holder.ivAvater.setImageURI(Uri.parse(location.user.getAvaterUrl()));
                } else {
                    holder.ivAvater.setImageURI(null);
                }
                holder.ivAvater.setOnClickListener(this);
                holder.ivAvater.setTag(location);
            } else {
                holder.ivAvater.setImageURI(null);
            }
        }
    }

    @Override
    public void setContentsNum(Location s, Holder holder) {
        if (holder.location != null && s.getObjectId().equals(holder.location.getObjectId())) {
            if (s.contentNum != 0) {
                String contentNum = context.getString(R.string.location_has_create_num)
                        .replace(context.getString(R.string.location_has_create_num_xx), s.contentNum.toString());
                holder.tvContentsNum.setText(contentNum);
            } else {
                holder.tvContentsNum.setText(null);
            }
        }
    }

    @Override
    public void showError(String message) {
        ToastUtil.showCustomToastInRelease(App.application, message);
    }

    @Override
    public void deleteSucess(Attention attention) {
        ToastUtil.showCustomToastInRelease(App.application, "删除成功");
        attentions.remove(attention);
        notifyDataSetChanged();
    }

    @Override
    public void showLoading(boolean isShow) {
        ((BaseActivity) context).showLoading(isShow);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_location_list_delete:
                if (v.getTag() != null) {
                    Attention attention = (Attention) v.getTag();
                    presenter.deleteAttention(attention);
                }
                break;
            case R.id.tv_location_list_share:
                if (v.getTag() != null) {
                    Attention attention = (Attention) v.getTag();
                    Intent intent = new Intent(context, SelectContactActivity.class);
                    intent.putExtra(ChatActivity.INTENT_ID, ChatActivity.SHARE);
                    intent.putExtra(ChatActivity.LOCATION_ID, attention.getLocation().getObjectId().toString());
                    intent.putExtra(ChatActivity.LOCATION_NAME, attention.getLocation().getLocationName());
                    context.startActivity(intent);
                }
                break;
            case R.id.iv_frag_location_list_avater:
                if (v.getTag() != null) {
                    Location location = (Location) v.getTag();
                    if (location.user != null) {
                        Intent intent = new Intent(context, ProfileActivity.class);
                        intent.putExtra(ProfileActivity.USER_ID, location.user.getObjectId());
                        context.startActivity(intent);
                    }
                }
                break;
            case R.id.ll_location_content:
                if (v.getTag() != null) {
                    Location location = (Location) v.getTag();
                    Intent intent = new Intent(context, ChannelActivity.class);
                    intent.putExtra(ChannelActivity.LOCATION_ID, location.getObjectId());
                    intent.putExtra(ChannelActivity.LOCATION_NAME, location.getLocationName());
                    context.startActivity(intent);
                }
                break;
        }
    }

    @Override
    public void fillValues(int position, View convertView) {
        Holder holder = (Holder) convertView.getTag();
        Location location = attentions.get(position).getLocation();
        holder.tvLocationName.setText(location.getLocationName());
        holder.location = location;
        if (location.contentNum != null) {
            String contentNum = context.getString(R.string.location_has_create_num)
                    .replace(context.getString(R.string.location_has_create_num_xx), location.contentNum.toString());
            holder.tvContentsNum.setText(contentNum);
        } else {
            presenter.requestContentNum(location, holder);
        }
        if (location.user != null) {
            if (location.user.getAvaterUrl() != null) {
                holder.ivAvater.setImageURI(Uri.parse(location.user.getAvaterUrl()));
            } else {
                holder.ivAvater.setImageURI(null);
            }
            holder.ivAvater.setOnClickListener(this);
            holder.ivAvater.setTag(location);
        } else {
            holder.ivAvater.setImageURI(null);
            presenter.requestAvater(location, holder);
        }
        holder.tvDelete.setOnClickListener(this);
        holder.tvShare.setOnClickListener(this);
        holder.tvShare.setTag(attentions.get(position));
        holder.tvDelete.setTag(attentions.get(position));
        holder.llContent.setOnClickListener(this);
        holder.llContent.setTag(attentions.get(position).getLocation());
    }

    @Override
    public int getCount() {
        return attentions.size();
    }

    @Override
    public Object getItem(int position) {
        return attentions.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void changeList(List<Attention> attentions) {
        this.attentions = attentions;
        notifyDataSetChanged();
    }

    public void onDestroy() {
        attentions = null;
        context = null;
        if (presenter != null)
            presenter.onDestroy();
        presenter = null;
    }

    public class Holder {
        @FindView(R.id.tv_location_list_delete)
        public TextView tvDelete;
        @FindView(R.id.tv_location_list_share)
        public TextView tvShare;
        @FindView(R.id.ll_attention_drag)
        public LinearLayout llDrag;
        @FindView(R.id.swipe_frag_session)
        public SwipeLayout swipeLayout;
        @FindView(R.id.iv_frag_location_list_avater)
        SimpleDraweeView ivAvater;
        @FindView(R.id.tv_frag_location_list_name)
        TextView tvLocationName;
        @FindView(R.id.tv_frag_location_list_contents_num)
        TextView tvContentsNum;
        @FindView(R.id.ll_location_content)
        LinearLayout llContent;
        Location location;
    }
}
